﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using sgsBase; 

namespace wfTree
{
	public partial class frmUserDetail : Form
	{
		public Relationship Relation = Relationship.Unknown;
		public DbFtPerson PersonRelatedTo = null;
		public DbFtPerson PersonEdit = null;

		public frmUserDetail()
		{
			InitializeComponent();
		}

		private void btOk_Click(object sender, EventArgs e)
		{
			if (PersonEdit == null)
				PersonEdit = new DbFtPerson();

			moveFromScreen(PersonEdit);

			if (Relation == Relationship.Primary)
			{
				PersonEdit.Status = 1;
				PersonEdit.InsertUpdate();
			}
			else if (Relation == Relationship.Spouse)
			{
				PersonEdit.SpousePersonId = PersonRelatedTo.PersonId;
				PersonEdit.Status = 1;
				PersonEdit.InsertUpdate();

				PersonRelatedTo.SpousePersonId = PersonEdit.PersonId;
				PersonRelatedTo.Update();
			}
			else if (Relation == Relationship.Father)
			{
				PersonEdit.PersonId = PersonRelatedTo.FatherPersonId;
				PersonEdit.Status = 1;
				PersonEdit.Update();
			}
			this.Close();
		}

		private void btCancel_Click(object sender, EventArgs e)
		{
			PersonEdit = null;
			PersonRelatedTo = null;
			this.Close();
		}

		private void frmUserDetail_Load(object sender, EventArgs e)
		{
			if (Relation == Relationship.Spouse && PersonRelatedTo != null)
			{
				PersonEdit = new DbFtPerson();
			}
			else if (PersonEdit != null)
			{
				moveToScreen(PersonEdit);
			}
		}

		private void moveToScreen(DbFtPerson person)
		{
			txFirstName.Text = person.FirstName;
			txLastName.Text = person.LastName;
			txMiddleName.Text = person.MiddleName;
			dtBirth.Value = person.DtBirth;
			if (person.Status == 3)
			{
				ckDeceased.Checked = true;
				if (person.DtDeath > DateTime.MinValue)
					dtDeath.Value = person.DtDeath;
				showDtDeath(true);
			}
			else
			{
				showDtDeath(false);
			}
			dtDeath.Visible = ckDeceased.Checked;
			if (person.Gender == 0)
				rbFemale.Checked = true;
		}

		private void moveFromScreen(DbFtPerson person)
		{
			person.FirstName = txFirstName.Text;
			person.LastName = txLastName.Text;
			person.MiddleName = txMiddleName.Text;
			person.DtBirth = dtBirth.Value;
			if (ckDeceased.Checked == true)
			{
				person.Status = 3;
				person.DtDeath = dtDeath.Value;
			}
			person.Gender = CASTHelper.Integer(rbMale.Checked);
		}

		private void showDtDeath(bool visible)
		{
			dtDeath.Visible = visible;
			lbDeath.Visible = visible;
		}

		private void ckDeceased_CheckedChanged(object sender, EventArgs e)
		{
			dtDeath.Visible = ckDeceased.Checked;
		}

		private void pbSpouse_Click(object sender, EventArgs e)
		{

		}
	}
}
